<?php
/**
 * @author 	PrestaLive <contact@prestalive.com>
 * @copyright  2011 PrestaLive
 * @version  	1.0 
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
class TabClass extends ObjectModel
{
	public		$id;
	public		$active = true;
	public		$position;
	public		$disable_tab = false;
	//lang
	public      $name;
	public      $description;

	protected 	$fieldsRequired = array('active');
 	protected 	$fieldsSize = array('active' => 1);
 	protected 	$fieldsValidate = array('active' => 'isBool');
	protected	$fieldsRequiredLang = array('name');
	protected	$fieldsSizeLang = array('name' => 128);
	protected	$fieldsValidateLang = array('name' => 'isGenericName', 'description' => 'isCleanHtml');
	
	protected 	$table = 'pl_at_tab';
	protected	$identifier = 'id_pl_at_tab';

	/**
	 * Prepare fields for ObjectModel class (add, update)
	 * All fields are verified (pSQL, intval...)
	 * @view ObjectModel::getFields()
	 *
	 * @return array All object fields
	 */
	public	function getFields()
	{
	 	parent::validateFields(false);
		$fields['active'] = (int)($this->active);
		$fields['disable_tab'] = (int)($this->disable_tab);
		$fields['position'] = (int)($this->position);
		return $fields;
	}	
	
	/**
	 * Check then return multilingual fields for database interaction
	 *
	 * @return array Multilingual fields
	 */
	public function getTranslationsFieldsChild()
	{
		parent::validateFieldsLang();
		return parent::getTranslationsFields(array('name', 'description'));
	}
	
	/**
	 * Add current object with position to database
	 * @view ObjectModel::add()
	 *
	 * return boolean Insertion result
	 */
	public function add($autodate = true, $nullValues = false)
	{
		$this->position = self::getLastPosition();
		return parent::add($autodate);
	}
	
	public static function getLastPosition()
	{
		return Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_.'pl_at_tab`');
	}
	
	/**
	 * Update position
	 * 
	 * @param int $way
	 * @param boolean $position
	 */
	public function updatePosition($way, $position)
	{
		if (!$res = Db::getInstance()->ExecuteS('
			SELECT t.`id_pl_at_tab`, t.`position`
			FROM `'._DB_PREFIX_.'pl_at_tab` t
			ORDER BY t.`position` ASC'))
				return false;

		foreach ($res AS $tab)
			if ((int)($tab['id_pl_at_tab']) == (int)($this->id))
				$movedTab = $tab;

		if (!isset($movedTab) || !isset($position))
			return false;
		// < and > statements rather than BETWEEN operator
		// since BETWEEN is treated differently according to databases
		$result = (Db::getInstance()->Execute('
			UPDATE `'._DB_PREFIX_.'pl_at_tab`
			SET `position`= `position` '.($way ? '- 1' : '+ 1').'
			WHERE `position`
			'.($way
				? '> '.(int)$movedTab['position'].' AND `position` <= '.(int)$position
				: '< '.(int)$movedTab['position'].' AND `position` >= '.(int)$position))
		AND Db::getInstance()->Execute('
			UPDATE `'._DB_PREFIX_.'pl_at_tab`
			SET `position` = '.(int)($position).'
			WHERE `id_pl_at_tab`='.(int)($movedTab['id_pl_at_tab'])));
		return $result;
	}
	
	/**
	 * Return available tabs
	 *
	 * @param integer $id_lang Language ID
     * @param boolean $active return only active tabs
	 * @return array Tabs
	 */
	public static function getTabs($active = false)
	{
		global $cookie;
		$tabs = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
		SELECT tl.name, tl.id_pl_at_tab
		FROM `'._DB_PREFIX_.'pl_at_tab` t
		LEFT JOIN `'._DB_PREFIX_.'pl_at_tab_lang` tl ON (t.`id_pl_at_tab` = tl.`id_pl_at_tab` AND tl.`id_lang` = '.(int)$cookie->id_lang.')
		WHERE 1
		'.($active ? 'AND t.active = 1' : '').'
		ORDER BY t.position ASC');
		
		return $tabs;
	}
	
	public static function getTabsAllow($idProduct)
	{
		//var_dump($idProduct);die();
		
		global $cookie;
		$tabs = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
		SELECT tl.name, tl.id_pl_at_tab, t.disable_tab
		FROM `'._DB_PREFIX_.'pl_at_tab` t
		LEFT JOIN `'._DB_PREFIX_.'pl_at_tab_lang` tl ON (t.`id_pl_at_tab` = tl.`id_pl_at_tab` AND tl.`id_lang` = '.(int)$cookie->id_lang.')
		WHERE t.active = 1
		ORDER BY t.position ASC');
		
		$tabs_arr = $tabs;
		$tabs_arr= array();
		foreach ($tabs as $tab) {
			if($tab['disable_tab']) {
				$attribute = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
					SELECT aa.id_pl_at_attribute
					FROM `'._DB_PREFIX_.'pl_at_attribute` aa
					LEFT JOIN `'._DB_PREFIX_.'pl_at_attribute_lang` aal ON (aa.`id_pl_at_attribute` = aal.`id_pl_at_attribute` AND aal.`id_lang` = '.(int)$cookie->id_lang.')
					WHERE aa.active = 1 AND aa.id_pl_at_tab = '.$tab['id_pl_at_tab'].'
					');
				
				include_once(_PS_MODULE_DIR_.'plattributetab/classes/AttributeClass.php');
				$attributeClass = new AttributeClass();
				$flag = false;
				foreach ($attributeClass->getAttributesByTabId($tab['id_pl_at_tab'], true) as $attribute) {
					$fieldValue = $attributeClass->getValueAttributes($idProduct, $attribute['field']);
					if ($fieldValue[$cookie->id_lang]) {
//						var_dump($fieldValue[$cookie->id_lang]);die();
						$flag = true;	
					};
				}
				
				if(count($attribute) AND $flag) {
					$tabs_arr[] = array('name' => $tab['name'],
										'id_pl_at_tab' => $tab['id_pl_at_tab']);
				} 
			}else {
				$tabs_arr[] = array('name' => $tab['name'],
									'id_pl_at_tab' => $tab['id_pl_at_tab']);
			}
		}
		return $tabs_arr;
	}
	
}